package lion.framework.core.install;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import lion.dev.lang.MapJ;
import lion.framework.core.conf.Config;
import lion.framework.core.conf.ConfigManager;

/**
 * 系统安装器
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年3月18日
 */

public class SystemInstaller {

	private static List<IInstall> installs = new ArrayList<>();
	private static boolean inited;
	private static boolean isInstalled = false;
	private static Log log = LogFactory.getLog(SystemInstaller.class);

	public static void addInstaller(IInstall install) {

		installs.add(install);
	}

	public static void install(MapJ param) throws Exception {

		if (isInstalled()) { return; }

		for (IInstall install : installs) {
			log.debug("install " + install.getName());
			install.install(param);
		}
		Config config = ConfigManager.getConfig("framework");
		config.put("framework.installed", true);
		config.save();
	}

	public static boolean isInstalled() {

		if (!inited) {
			init();
		}
		return isInstalled;
	}

	private static void init() {

		Config config = ConfigManager.getConfig("framework");
		String installed = config.getString("framework.installed");
		isInstalled = StringUtils.isBlank(installed) || "true".equals(installed);
	}
}
